library(opendatascot)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3 ✓ purrr 0.3.4
✓ tibble 3.0.6 ✓ dplyr 1.0.2
✓ tidyr 1.1.2 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
ods_all_datasets() %>%
janitor::clean_names() %>%
filter(name == "Life Expectancy") %>%
select(uri)
ods_structure("healthy-life-expectancy")
$schemes
[1] "http://purl.org/linked-data/sdmx/2009/dimension#refArea"
[2] "http://purl.org/linked-data/sdmx/2009/dimension#refPeriod"
[3] "http://purl.org/linked-data/cube#measureType"
[4] "http://statistics.gov.scot/def/dimension/sex"
[5] "http://statistics.gov.scot/def/dimension/age"
[6] "http://statistics.gov.scot/def/dimension/simdQuintiles"
[7] "http://statistics.gov.scot/def/dimension/urbanRuralClassification"
$categories
$categories$refArea
[1] "S12000005" "S12000006" "S12000008" "S12000010" "S12000011" "S12000013" "S12000014" "S12000047" "S12000017"
[10] "S12000018" "S12000019" "S12000020" "S12000021" "S12000023" "S12000048" "S12000026" "S12000027" "S12000028"
[19] "S12000029" "S12000030" "S12000033" "S12000034" "S12000035" "S12000036" "S12000038" "S12000039" "S12000040"
[28] "S12000041" "S12000042" "S12000050" "S12000045" "S12000049" "S92000003" "S08000015" "S08000016" "S08000017"
[37] "S08000019" "S08000020" "S08000031" "S08000022" "S08000032" "S08000024" "S08000025" "S08000026" "S08000028"
[46] "S08000029" "S08000030"
$categories$refPeriod
[1] "P3Y" "P3Y" "P3Y"
$categories$measureType
[1] "count" "95-lower-confidence-limit" "95-upper-confidence-limit"
$categories$sex
[1] "male" "female"
$categories$age
[1] "0-years" "1-4-years" "5-9-years" "10-14-years" "15-19-years" "20-24-years" "25-29-years" "30-34-years"
[9] "35-39-years" "40-44-years" "45-49-years" "50-54-years" "55-59-years" "60-64-years" "65-69-years" "70-74-years"
[17] "75-79-years" "80-84-years" "85-89-years" "90-years"
$categories$simdQuintiles
[1] "all" "1-most-deprived" "2" "3" "4"
[6] "5-least-deprived"
$categories$urbanRuralClassification
[1] "all" "large-urban-areas" "other-urban-areas" "accessible-small-towns"
[5] "remote-small-towns" "accessible-rural" "remote-rural"
hle_structure <- ods_structure("healthy-life-expectancy")
hle_structure$categories$refPeriod <- c("2015-2017", "2016-2018", "2017-2019")
le_structure <- ods_structure("Life-Expectancy")
le_structure$categories$refPeriod
[1] "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y"
[19] "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y"
ods_dataset("homelessness-applications",
applicationType = "all-applications")
ods_structure("healthy-life-expectancy")$categories$refPeriod
[1] "P3Y" "P3Y" "P3Y"
ods_structure("Life-Expectancy")$categories$refPeriod
[1] "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y"
[19] "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y" "P3Y"
ods_structure("homelessness-applications")$categories$refPeriod
[1] "2007-2008" "2008-2009" "2009-2010" "2010-2011" "2011-2012" "2012-2013" "2013-2014" "2014-2015" "2015-2016"
[10] "2016-2017" "2017-2018" "2018-2019" "2019-2020"
ods_dataset("healthy-life-expectancy",
refPeriod = "P3Y",
age = "0-years",
simdQuintiles = "all",
urbanRuralClassification = "all",
measureType = "count") %>%
filter(age == "0-years") %>%
select(-c(simdQuintiles, measureType, urbanRuralClassification, age)) %>%
filter(refArea == "S92000003")
library(rgdal)
Loading required package: sp
rgdal: version: 1.5-18, (SVN revision 1082)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.1.4, released 2020/10/20
Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
Linking to sp version:1.4-4
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
hb_shapes <- readOGR(
dsn ="data/simplified_shapefiles/health_boards/NHS_HealthBoards_2019/",
layer = "NHS_HealthBoards_2019",
GDAL1_integer64_policy = TRUE)
Discarded datum OSGB_1936 in CRS definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
OGR data source with driver: ESRI Shapefile
Source: "/Users/user/scot_stats_dashboard/data/simplified_shapefiles/health_boards/NHS_HealthBoards_2019", layer: "NHS_HealthBoards_2019"
with 14 features
It has 4 fields
library(rgeos)
rgeos version: 0.5-5, (SVN revision 640)
GEOS runtime version: 3.8.1-CAPI-1.13.3
Linking to sp version: 1.4-2
Polygon checking: TRUE
hb_simple <- gSimplify(hb_shapes, tol = 0.025, topologyPreserve = TRUE)
56.3950° N, 3.4308° W
uk_shape <- readOGR(
dsn ="../Downloads/scot_stats_dashboard/bdline_essh_gb/Data/GB/",
layer = "county_region",
GDAL1_integer64_policy = TRUE)
Discarded datum OSGB_1936 in CRS definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
OGR data source with driver: ESRI Shapefile
Source: "/Users/user/Downloads/scot_stats_dashboard/bdline_essh_gb/Data/GB", layer: "county_region"
with 26 features
It has 15 fields
Integer64 fields read as doubles: NUMBER NUMBER0 POLYGON_ID UNIT_ID
uk_simple <- gSimplify(uk_shape, tol = 0.025, topologyPreserve = TRUE)
plot(uk_simple)
library(maps)
Attaching package: ‘maps’
The following object is masked from ‘package:purrr’:
map
UK <- map_data("world") %>% filter(region == "UK")
UK
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
country_uk <- world %>%
filter(name == "United Kingdom")
poly_uk <- as(country_uk, 'Spatial')
Discarded datum Unknown based on WGS84 ellipsoid in CRS definition
poly_uk@polygons
library(broom)
hle_map_data <- spdf_fortified %>%
left_join(hb_lookup, by = c("id" = "id")) %>%
left_join(hle_data, by = c("hb_name" = "reference_area"))
library(plotly)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
filtered_hle_data <- hle_map_data %>%
filter(ref_period == "2015-2017") %>%
filter(sex =="male")
plotly::ggplotly(
ggplot() +
geom_polygon(data = hle_map_data,
aes(x = long, y = lat, group = group,
fill = healthy_life_expectancy,
text = paste0(hb_name, ": ", healthy_life_expectancy,"years")),
colour = "white", size = 0.25) +
xlim(-7.7,-0.3) +
scale_fill_viridis_c() +
coord_map() +
theme_void(),
tooltip = "text"
) %>% config(displayModeBar = FALSE) %>%
layout(xaxis = list(fixedrange = TRUE)) %>%
layout(yaxis = list(fixedrange = TRUE))
Ignoring unknown aesthetics: text
my_map
hb_shapes@data <- hb_shapes@data %>%
janitor::clean_names() %>%
left_join(hle_2015_m, by = c("hb_name" = "reference_area"))
labels <- sprintf("<strong>%s</strong><br/>%g years",
hb_shapes$hb_name, hb_shapes$healthy_life_expectancy) %>%
lapply(htmltools::HTML)
# add Health Board polygons, colour based on LE, highlight on hover
my_map %>%
addPolygons(data = hb_shapes, color = "white",
fillColor = ~colorQuantile(
"YlOrRd", (-hb_shapes$healthy_life_expectancy))
(-hb_shapes$healthy_life_expectancy),
weight = 1, fillOpacity = 0.9, label = labels,
highlightOptions = highlightOptions(
color = "black", weight = 2,
opacity = 0.9, bringToFront = TRUE))